package com.zee.wechat.module.menu.service;

import com.alibaba.fastjson.JSONObject;
import com.zee.wechat.framework.utils.HttpRequest;
import com.zee.wechat.module.menu.entity.Button;
import com.zee.wechat.module.menu.entity.CommonButton;
import com.zee.wechat.module.menu.entity.ComplexButton;
import com.zee.wechat.module.menu.entity.Menu;
import com.zee.wechat.module.menu.mapper.MenuMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class CreatWechatMenuService {
    @Resource
    private MenuMapper menuDao;

    /**
     * @function 通过微信获得access_token
     * @author Zee
     * @date 2016年11月29日
     */
    public JSONObject getWechatAccessToken(String appid, String secret) {
        String wechatUserInfoJSON = "";
        if (null != appid && !"".equals(appid) && null != secret
                && !"".equals(secret)) {
            // 通过当前微信用户ID取得微信用户的信息
            try {
                wechatUserInfoJSON = HttpRequest
                        .httpRequestGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                                + appid + "&secret=" + secret);
            } catch (Exception e) {
                e.printStackTrace();
            }
            JSONObject result = JSONObject.parseObject(wechatUserInfoJSON);
            return result;
        } else {
            return null;
        }

    }

    /**
     * 创建菜单
     *
     * @param menu
     * @param accessToken
     * @return
     * @throws Exception
     * @author Zee
     * @date 2016年11月29日
     */
    public String createMenu(Menu menu, String accessToken) throws Exception {
        String menu_create_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="
                + accessToken;
        // 拼装创建菜单的url
        String json = JSONObject.toJSONString(menu);
        // 调用接口创建菜单
        return HttpRequest.httpWechatRequest(menu_create_url, "POST", json);

    }

    /**
     * 获取menuList
     *
     * @param accessToken
     * @return
     * @throws Exception
     */
    public String getMenuList(String accessToken) throws Exception  {
        String menuListUrl = " https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + accessToken;
        return HttpRequest.httpRequestGet(menuListUrl);
    }

    /**
     * 组装菜单数据
     *
     * @return
     * @author Zee
     */
    public Menu getMenu(String wechatId) {
        List<Map<String, Object>> menus = menuDao.queryMenus(wechatId);
        Button[] mainButtons = new Button[menus.size()];

        for (int i = 0; i < menus.size(); i++) {
            Map<String, Object> mainMenu = (Map<String, Object>) menus.get(i);
            ComplexButton mainBtn = new ComplexButton();
            List<Map<String, Object>> subMenus = menuDao.querySubMenus(String
                    .valueOf(mainMenu.get("code")));
            CommonButton[] commonButtons = new CommonButton[subMenus.size()];
            for (int j = 0; j < subMenus.size(); j++) {
                CommonButton subBtn = new CommonButton();
                Map<String, Object> subMenu = (Map<String, Object>) subMenus
                        .get(j);
                subBtn.setName(subMenu.get("name") + "");
                subBtn.setType(subMenu.get("type") + "");
                if ("click".equals(subMenu.get("type"))) {
                    subBtn.setKey(subMenu.get("sKey") + "");
                } else if ("view".equals(subMenu.get("type"))) {
                    subBtn.setUrl(subMenu.get("url") + "");
                }
                commonButtons[j] = subBtn;
            }
            mainBtn.setName(mainMenu.get("name") + "");
            if ("no".equals(mainMenu.get("hasSubMenu"))) {
                mainBtn.setSub_button(commonButtons);
            } else {

                mainBtn.setType(mainMenu.get("type") + "");
                if ("click".equals(mainMenu.get("type"))) {
                    mainBtn.setKey(mainMenu.get("mKey") + "");
                } else if ("view".equals(mainMenu.get("type"))) {
                    mainBtn.setUrl(mainMenu.get("url") + "");
                }
            }
            mainButtons[i] = mainBtn;

        }
        Menu menu = new Menu();
        menu.setButton(mainButtons);
        return menu;
    }
}
